from fastapi import APIRouter, Depends, HTTPException, status
from typing import Dict, List, Any, Optional
import os

# 创建API路由器
api_router = APIRouter(
    prefix="/api/v1",
    tags=["api"],
    responses={404: {"description": "Not found"}},
)

# 获取API信息
@api_router.get("/info")
async def get_api_info() -> Dict[str, Any]:
    """
    获取API信息
    """
    return {
        "name": "FastAPI应用 API",
        "version": "1.0.0",
        "status": "active",
        "environment": os.getenv("ENVIRONMENT", "development")
    }

# 示例数据
items_db = [
    {"id": 1, "name": "项目1", "description": "这是项目1的描述"},
    {"id": 2, "name": "项目2", "description": "这是项目2的描述"},
    {"id": 3, "name": "项目3", "description": "这是项目3的描述"},
]

# 获取所有项目
@api_router.get("/items", response_model=List[Dict[str, Any]])
async def get_items() -> List[Dict[str, Any]]:
    """
    获取所有项目
    """
    return items_db

# 获取单个项目
@api_router.get("/items/{item_id}", response_model=Dict[str, Any])
async def get_item(item_id: int) -> Dict[str, Any]:
    """
    根据ID获取项目
    """
    for item in items_db:
        if item["id"] == item_id:
            return item
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail=f"项目ID {item_id} 不存在"
    ) 